package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap.innerService.impl;

import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap.MnSapIfSOModifyServiceImpl;
import com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap.innerService.FulfillmentOrderService;
import com.alibaba.cirtus.ots.application.adapter.ability.model.api.BcFulfillmentOrderDetailSearchResponse;
import com.alibaba.cirtus.ots.application.adapter.ability.model.api.BcFulfillmentOrderDetailSearchResponseResult;
import com.alibaba.cirtus.ots.application.adapter.ability.model.api.SearchOrderOpenApiReq;
import com.alibaba.cirtus.ots.application.adapter.ability.utils.OutOpenapiHttpUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@Primary
public class FulfillmentOrderServiceImpl implements FulfillmentOrderService {
    private final Log log = Log.getLogger(FulfillmentOrderServiceImpl.class);
    private static final String SEARCH_ORDER_URL = "/delivery/dubbo/api/fulfillment/searchList";
    @Resource
    private OutOpenapiHttpUtils outOpenapiHttpUtils;
    @Override
    public List<BcFulfillmentOrderDetailSearchResponse> getBcFulfillmentOrderDetail(Order order) {

        int pageSize =100;
        SearchOrderOpenApiReq searchOrderOpenApiReq = new SearchOrderOpenApiReq();
        searchOrderOpenApiReq.setBusinessType(String.valueOf(order.getBusinessType()));
        searchOrderOpenApiReq.setSourceOrderNo(order.getOrderBizId());
        searchOrderOpenApiReq.setPage(1);
        searchOrderOpenApiReq.setSize(pageSize);
        ResponseEntity<BcFulfillmentOrderDetailSearchResponseResult> outResult = outOpenapiHttpUtils.post(SEARCH_ORDER_URL, JSON.toJSON(searchOrderOpenApiReq), BcFulfillmentOrderDetailSearchResponseResult.class);
        log.info("MnSapIf013V2ServiceImpl#doModifyAmount#outResult={}",JSON.toJSONString(outResult));
        if(Objects.isNull(outResult) || Objects.isNull(outResult.getBody())){
            return new ArrayList<>();
        }
        List<BcFulfillmentOrderDetailSearchResponse> result = outResult.getBody().getResult();
        Integer total = outResult.getBody().getTotal();
        int size = outResult.getBody().getResult().size();
        if (total>size){
            int remain = total-size;
            int page  =  remain %pageSize ==0? (remain/pageSize):(remain/pageSize+1);
            for (int i = 1; i <= page; i++) {
                searchOrderOpenApiReq.setPage(i+1);
                ResponseEntity<BcFulfillmentOrderDetailSearchResponseResult> pageOutResult = outOpenapiHttpUtils.post(SEARCH_ORDER_URL, JSON.toJSON(searchOrderOpenApiReq), BcFulfillmentOrderDetailSearchResponseResult.class);
                log.info("MnSapIf013V2ServiceImpl#doModifyAmount#pageOutResult={}",JSON.toJSONString(pageOutResult));
                if(Objects.isNull(pageOutResult) || Objects.isNull(pageOutResult.getBody())){
                    break;
                }
                result.addAll(pageOutResult.getBody().getResult());
            }
        }
        return result;
    }

    @Override
    public List<BcFulfillmentOrderDetailSearchResponse> getEffectiveBcFulfillmentOrderDetail(Order order) {
        String[] filterStatus = {"1","7"};
        List<String> filterList = Arrays.asList(filterStatus);
        List<BcFulfillmentOrderDetailSearchResponse> searchResponse = getBcFulfillmentOrderDetail(order);
        return searchResponse.stream().filter(it->!filterList.contains(it.getDetailStatus()) && "1".equals(it.getFulfillmentDirectionType())).collect(Collectors.toList());

    }

    @Override
    public List<BcFulfillmentOrderDetailSearchResponse> getBcFulfillmentOrderDetail(Order order, String fulfillmentOrderNo) {
        int pageSize =100;
        SearchOrderOpenApiReq searchOrderOpenApiReq = new SearchOrderOpenApiReq();
        searchOrderOpenApiReq.setBusinessType(String.valueOf(order.getBusinessType()));
        searchOrderOpenApiReq.setFulfillmentOrderNo(fulfillmentOrderNo);
        searchOrderOpenApiReq.setPage(1);
        searchOrderOpenApiReq.setSize(pageSize);
        ResponseEntity<BcFulfillmentOrderDetailSearchResponseResult> outResult = outOpenapiHttpUtils.post(SEARCH_ORDER_URL, JSON.toJSON(searchOrderOpenApiReq), BcFulfillmentOrderDetailSearchResponseResult.class);
        log.info("MnSapIf013V2ServiceImpl#doModifyAmount#outResult={}",JSON.toJSONString(outResult));
        if(Objects.isNull(outResult) || Objects.isNull(outResult.getBody())){
            return new ArrayList<>();
        }
        List<BcFulfillmentOrderDetailSearchResponse> result = outResult.getBody().getResult();
        Integer total = outResult.getBody().getTotal();
        int size = outResult.getBody().getResult().size();
        if (total>size){
            int remain = total-size;
            int page  =  remain %pageSize ==0? (remain/pageSize):(remain/pageSize+1);
            for (int i = 1; i <= page; i++) {
                searchOrderOpenApiReq.setPage(i+1);
                ResponseEntity<BcFulfillmentOrderDetailSearchResponseResult> pageOutResult = outOpenapiHttpUtils.post(SEARCH_ORDER_URL, JSON.toJSON(searchOrderOpenApiReq), BcFulfillmentOrderDetailSearchResponseResult.class);
                log.info("MnSapIf013V2ServiceImpl#doModifyAmount#pageOutResult={}",JSON.toJSONString(pageOutResult));
                if(Objects.isNull(pageOutResult) || Objects.isNull(pageOutResult.getBody())){
                    break;
                }
                result.addAll(pageOutResult.getBody().getResult());
            }
        }
        return result;
    }

    @Override
    public List<BcFulfillmentOrderDetailSearchResponse> getEffectiveBcFulfillmentOrderDetail(Order order, String fulfillmentOrderNo) {
        String[] filterStatus = {"1","7"};
        List<String> filterList = Arrays.asList(filterStatus);
        List<BcFulfillmentOrderDetailSearchResponse> searchResponse = getBcFulfillmentOrderDetail(order,fulfillmentOrderNo);
        return searchResponse.stream().filter(it->!filterList.contains(it.getDetailStatus()) && "1".equals(it.getFulfillmentDirectionType())).collect(Collectors.toList());

    }
}
